﻿#include "gamearea.h"

GameArea::GameArea(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(COLUMN_COUNT*IMAGE_WIDHT,ROW_COUNT*IMAGE_HEIGHT);
}
void GameArea::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QPainter painter(this);
    if(showGameArea)
    {
        for(int i=0;i<ROW_COUNT;++i)
            for(int j=0;j<COLUMN_COUNT;++j)
            {
                if(!(*isEmpty)[i][j])
                painter.drawPixmap(j*IMAGE_WIDHT,i*IMAGE_HEIGHT,IMAGE_WIDHT,IMAGE_HEIGHT,
                                   QPixmap(QString(":/image/%1.png").arg(PixmapData[i][j])));
            }
    }
}
void GameArea::newGame()
{
    showGameArea=true;
    update();
}
bool GameArea::canBeLinked(int row1, int col1, int row2, int col2)
{
   return inOneLine(row1, col1, row2, col2)||inTwoLine(row1, col1, row2, col2);
}

bool GameArea::inOneLine(int row1, int col1, int row2, int col2)
{
    bool flag=true;
    if(row1==row2)
    {
        for(int i=qMin(col1,col2)+1;i<qMax(col1,col2);++i){
            if(!(*isEmpty)[row1][i])
            {
                flag=false;
                break;
            }
        }
        if(flag)
            return true;
    }
    else if(col1==col2)
    {
        for(int i=qMin(row1,row2)+1;i<qMax(row1,row2);i++)
        {
            qDebug()<<i<<","<<col1<<" "<<(*isEmpty)[row1][i];
            if(!(*isEmpty)[i][col1])
            {
                return false;
            }
        }

        return true;
    }
    return false;
}

bool GameArea::inTwoLine(int row1, int col1, int row2, int col2)
{
    bool flag1=inOneLine(row1,col1,row1,col2)&&inOneLine(row1,col2,row2,col2);
    bool flag2=inOneLine(row1,col1,row2,col1)&&inOneLine(row2,col1,row2,col2);

    return ( flag1 && (*isEmpty)[row1][col2] ) || ( flag2 && (*isEmpty)[row2][col1] );

}
int GameArea::getProgress()
{
    double done=0;
    for(int i=0;i<ROW_COUNT;++i)
        for(int j=0;j<COLUMN_COUNT;++j)
        {
            if((*isEmpty)[i][j])
                done++;
        }
    return static_cast<int>(done/ROW_COUNT/COLUMN_COUNT*100);
}
void GameArea::mousePressEvent(QMouseEvent *e)
{
    int currentX=e->x()/IMAGE_WIDHT;
    int currentY=e->y()/IMAGE_HEIGHT;
    if((*isEmpty)[currentY][currentX])
        return ;
    ++clickCount;
    if(clickCount%2!=0)
    {
        firstClickX=currentX;
        firstClickY=currentY;
        qDebug()<<"第一次点击"<<firstClickY<<","<<firstClickX<<"图片类型是"<<PixmapData[currentY][currentX];
    }

    else if(clickCount%2==0){

        qDebug()<<"第二次点击"<<currentY<<" ,"<<currentX<<"图片类型是"<<PixmapData[currentY][currentX];
        if(PixmapData[firstClickY][firstClickX]!=PixmapData[currentY][currentX]||
                (currentY==firstClickY&&currentX==firstClickX))
            return ;
        bool flag=canBeLinked(firstClickY,firstClickX,currentY,currentX);
        qDebug()<<"判断连接"<<flag;
        if(flag)
        {
            qDebug()<<"修改数组";
            (*isEmpty)[firstClickY][firstClickX]=true;
            (*isEmpty)[currentY][currentX]=true;
            update();
        }


    }

}

void GameArea::mouseReleaseEvent(QMouseEvent *e)
{
    Q_UNUSED(e);

    emit updateProgress();
    bool youWin=0;
    if(youWin)
    {
        emit hasWin();
        if(QMessageBox::Ok==QMessageBox::question(this,"You win","Congratulation! Do you want to continue the game",
                                                  QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Cancel))
        {
            emit restart();
        }
        else{
            emit winCancel();
        }
    }
}
